import { BaseMessage } from "../../message/base";
import { ModelBackend, ModelBackendConfig } from "../base";
import { RoleMessageType } from "../../enum";
import { SendingChatMessage } from "../../message/chatMessage";
import { defaultHumanConfig } from "./config";
import { TokenStatistical } from "../../token/tokenStatistical";

export class HumanBackend extends ModelBackend {
    constructor(config: ModelBackendConfig) {
        if (config.modelMaker !== "Human") {
            throw new Error("Invalid model maker for HumanBackend");
        }
        const humanConfig: ModelBackendConfig = {
            ...config,
            modelConfig: { ...defaultHumanConfig, ...config.modelConfig }
        };
        super(humanConfig);
    }

    async run(messages: BaseMessage[]): Promise<BaseMessage | undefined> {
        const response = { choices: [{ message: { content: "Hello, I am a human." } }] };
        const humanConfig = messages[0].getConfig();

        const content = response.choices[0].message.content;
        if (!content) {
            return;
        }
        return new SendingChatMessage({
            roleName: humanConfig.roleName,
            roleType: humanConfig.roleType,
            roleMessageType: RoleMessageType.AgentSending,
            content: content,
            metaDict: response
        });
    }
    getTokenLimit(): number {
        return Infinity;
    }
    getTokenNumberFromMessage(messages: BaseMessage[] | string): number {
        return TokenStatistical.getInstance().getTokenNumberFromMessage(messages);
    }
}
